import { FlatCompat } from '@eslint/eslintrc';
import importPlugin from 'eslint-plugin-import';
import oxlint from 'eslint-plugin-oxlint';
import promisePlugin from 'eslint-plugin-promise';
import securityPlugin from 'eslint-plugin-security';

const compat = new FlatCompat({
  baseDirectory: import.meta.dirname,
});

const eslintConfig = [
  ...compat.config({
    extends: ['next/core-web-vitals', 'next/typescript', 'prettier'],
  }),
  {
    plugins: {
      import: importPlugin,
      promise: promisePlugin,
      security: securityPlugin,
    },
    rules: {
      // Import 规则
      'import/order': [
        'error',
        {
          groups: ['builtin', 'external', 'internal', 'parent', 'sibling', 'index'],
          'newlines-between': 'always',
          alphabetize: { order: 'asc' },
        },
      ],
      'import/no-unresolved': 'off', // TypeScript 处理这个
      'import/no-duplicates': 'error',
      'import/no-unused-modules': 'error',

      // Promise 规则
      'promise/always-return': 'error',
      'promise/no-return-wrap': 'error',
      'promise/param-names': 'error',
      'promise/catch-or-return': 'error',
      'promise/no-nesting': 'warn',

      // Security 规则
      'security/detect-buffer-noassert': 'error',
      'security/detect-child-process': 'warn',
      'security/detect-disable-mustache-escape': 'error',
      'security/detect-eval-with-expression': 'error',
      'security/detect-no-csrf-before-method-override': 'error',
      'security/detect-non-literal-fs-filename': 'warn',
      'security/detect-non-literal-regexp': 'warn',
      'security/detect-object-injection': 'warn',
      'security/detect-possible-timing-attacks': 'warn',
      'security/detect-pseudoRandomBytes': 'error',
      'security/detect-unsafe-regex': 'error',
    },
  },
  {
    ignores: [
      '.next/**',
      'dist/**',
      'src/generated/**',
      'node_modules/**',
      '.spec-workflow/',
      'logs/**',
      'public/**',
      'coverage/**',
      'bun.lockb',
      '*.lock',
      '*.log',
      '.husky/**',
    ],
  },
  ...oxlint.configs['flat/recommended'],
];

export default eslintConfig;
